package rexsee.dialog;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.app.Activity;
import android.content.Context;

public class RexseeChronometer implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Chronometer";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeChronometer(childBrowser);
	}

	private final Context mContext;
	private final Browser mBrowser;
	private ChronometerDialog mClock = null;

	public RexseeChronometer(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public void show(final String style, final int timeout, final String timeoutCallback) {
		show(style, timeout, timeoutCallback, true, true, true, "#ff0000");
	}
	public void show(final String style, final int timeout, final String timeoutCallback, final boolean showTotal, final boolean showElapse, final boolean showRest, final String highlightColor) {
		dismiss();
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mClock = new ChronometerDialog(mBrowser, style, timeout, (timeoutCallback.equals("")) ? null : new Runnable() {
					@Override
					public void run() {
						mBrowser.function.load(timeoutCallback);
					}
				}, showTotal, showElapse, showRest, highlightColor);
				mClock.start();
			}
		});
	}
	public void dismiss() {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (mClock != null) mClock.dismiss();
				mClock = null;
			}
		});
	}
	public void setStyle(String style) {
		if (mClock == null) return;
		mClock.setStyle(style);
	}
	public String getStyle(String name) {
		return (mClock == null) ? "" : mClock.getStyle(name);
	}

}
